<?php
/**
 *  Panorama-Picture Plugin
 *
 *  @license    GPL 2 ( http://www.gnu.org/licenses/gpl.html )
 *  @author     jaloma.ac [at] googlemail [dot] com
 *
 *  Description:
 *  Plugin to display Panorama-Pictures with Java-Applet http://www.tshsoft.de/panostudioapplet/index.html
 *
 *  Acknowledgements:
 *  The java applet author(s)
 */

if (!defined('DOKU_INC'))define('DOKU_INC', realpath(dirname( __FILE__ ).'/../../').'/');
if (!defined('DOKU_PLUGIN'))define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
require_once (DOKU_PLUGIN.'syntax.php');
require_once(DOKU_PLUGIN.'panorama/render_helper.php');
/**
 *  Panorama Plugin
 */
class syntax_plugin_panorama_panorama extends DokuWiki_Syntax_Plugin
{

    var $dflt = array (
    'width'=>800,
    'height'=>400,
    'name'=>'',
    'mode'=>'java'
    );
    /**
     *  Plugin Info
     */
    function getInfo()
    {
        return array (
        'author'=>'Jürgen A. Lamers',
        'email'=>'jaloma.ac [at] googlemail [dot] com',
        'date'=>@file_get_contents(DOKU_PLUGIN . 'panorama/VERSION'),
        'name'=>'panorama',
        'desc'=>'Panorama-Picture Plugin <panorama name="" width="" height="" mode=""/>',
        'url'=>'http://www.dokuwiki.org/plugin:panorama',
        );
    }

    /**
     *  Typology?
     */
    function getType()
    {
        return 'substition';
    }

    /**
     *  Sort Code?
     */
    function getSort()
    {
        return 316;
    }

    /**
     *  Pattern Matching?
     */
    function connectTo($mode)
    {
        $this->Lexer->addSpecialPattern('<panorama.*?/>', $mode, 'plugin_panorama_panorama');
    }

    function matchLength()
    {
        return strlen("<panorama");
    }
    /**
     *  Parsing
     *  1. Very rough parsing involved
     *  2. Applet parameters not included
     */
    function handle($match, $state, $pos, & $handler)
    {
        $match = html_entity_decode(substr($match, $this->matchLength(), -2));
        $gmap = $this->_extract_params($match);
        return $gmap;
    }
    /**
     * extract parameters for the googlemap from the parameter string
     *
     * @param   string    $str_params   string of key="value" pairs
     * @return  array                   associative array of parameters key=>value
     */
    function _extract_params($str_params)
    {
        $param = array ();
        preg_match_all('/(\w*)="(.*?)"/us', $str_params, $param, PREG_SET_ORDER);
        if (sizeof($param) == 0)
        {
            preg_match_all("/(\w*)='(.*?)'/us", $str_params, $param, PREG_SET_ORDER);
        }
        // parse match for instructions, break into key value pairs
        $gmap = $this->dflt;
        foreach ($param as $kvpair)
        {
            list ($match, $key, $val) = $kvpair;
            if ( isset ($gmap[$key]))$gmap[$key] = $val;
        }

        return $gmap;
    }

    /**
     *  Rendering
     */
    function render($mode, & $renderer, $data)
    {
        if ($mode == 'xhtml')
        {
            global $conf;
            $mode = $data['mode'];
            $txt = "";
            if ($mode == null || $mode == 'java')
            {
                $txt = render_helper::_render_java($data);
            } elseif ($mode == 'swf' || $mode == 'flash')
            {
                $txt = render_helper::_render_swf($data);
            }
            if ($this->getConf('showPanoramaUrl'))
            {
                $txt .= '<br />Powered by <a href="http://www.tshsoft.com/panostudioapplet/index.html">PanoramaStudio Viewer</a>';
            }

            $renderer->doc .= $txt;
            return true;
        } else
        {
            return false;
        }
    }

}// class
